home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Software für Mac-OS X / Netze / iTools / installer.sh next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2001-07-30  |  23.9 KB  |  1,086 lines

  1. #! /bin/sh
  2.  
  3. # pmpkginstall: a poor man's installer script.
  4. #
  5. # This script does everything Installer.app does, except for fancy graphics
  6. # and getting the correct permissions to write a receipt (since we cannot
  7. # make it setuid just for that; an obvious fix is to rewrite it in Perl).
  8. #   Well, this may have changed: use --limitations to know for sure how 
  9. # this tool performs at a given time.
  10. #
  11. #
  12. # Yves Arrouye, 1997
  13.  
  14. : ${TMPDIR:=/tmp}
  15.  
  16. me=`basename $0`
  17.  
  18. version="1.4"
  19.  
  20. interactive=yes
  21. action=install
  22. receipt=1
  23. verbose=0
  24. dwiw=0
  25.  
  26. # Urk.
  27.  
  28. if [ -x /usr/bin/lsbom ]
  29. then
  30.     lsbom=/usr/bin/lsbom
  31. else
  32.     lsbom=/usr/etc/lsbom
  33. fi
  34.  
  35. #
  36.  
  37. which() {
  38.     for d in `echo $PATH | tr ':' ' '`
  39.     do
  40.     if [ -f ${d}/"$1" -a -x ${d}/"$1" ]
  41.     then
  42.         echo ${d}/"$1"
  43.         return
  44.     fi
  45.     done
  46. }
  47.  
  48. usage() {
  49.     bye=$1
  50.     : ${bye:=1}
  51.  
  52.     if [ $bye -eq 0 ]
  53.     then
  54.     echo -n U
  55.     else
  56.     echo -n u
  57.     exec >&2
  58.     fi
  59.  
  60.     echo "sage: $me [ --help ] [ --version ] [ --limitations ]"
  61.     echo "       $me [ --help ] [ --interactive | --automatic ] [ --verbose ] [ --install ] [ --dwiw ] [ --noreceipt ] package [ installdir ]"
  62.     echo "       $me [ --help ] [ --interactive | --automatic ] --delete [ --dwiw ] package"
  63.     echo "       $me [ --help ] --info package"
  64.     echo "       $me [ --help ] --list package"
  65.    
  66.     if [ $bye -eq 0 ]
  67.     then
  68.     cat <<EOF
  69.  
  70. Options: --help        print this help message
  71.      --version      print the version
  72.      --limitations  list limitations and differences with Installer.app
  73.      --interactive    ask if scripts are to be run (default)
  74.      --automatic    run scripts without asking
  75.      --verbose      do additional reporting
  76.      --install    install a package (default)
  77.      --dwiw         do what I want (even if it is forbidden; note that
  78.             using this option is asking for trouble...)
  79.      --noreceipt    do not create an installation receipt
  80.      --delete    delete a package
  81.      --info        print the information of the package
  82.      --list        list the contents of a package
  83. EOF
  84.     fi
  85.  
  86.     exit $bye
  87. }
  88.  
  89. limitations() {
  90.     cat <<EOF
  91. Known limitations:
  92.   1. One must install packages as root in order to be able to write
  93.      receipts in /Library/Receipts. (More exactly, one must have
  94.      the appropriate write permissions on this directory.) Use the
  95.      --noreceipt option if you do not want to create a receipt. On
  96.      the other hand, if you're installing a package in user space,
  97.      the receipt will go to ~/Library/Receipts and this is just what
  98.      you want...
  99.   2. Multiple-volumes packages on floppy disks are not handled.
  100.   3. If one installs packages without being root, the installation may
  101.      fail because some files and directories do not have the correct
  102.      permissions. It is recommend to first "remove" the package as
  103.      root (using the --delete --dwiw flags) and reinstalling again as
  104.      the user, or simply to install as root.
  105.  
  106.  
  107. Known differences with Installer.app:
  108.   1. The script does not check for files that will be overwritten.
  109.      This is a design choice!
  110.   2. It is possible to avoid answering questions about whether to run
  111.      scripts or not; $me can be invoked by other tools.
  112.   3. There are no fancy graphics.
  113. EOF
  114. }
  115.  
  116. bye() {
  117.     if [ "`2>/dev/null getinfokey DisableStop`" != YES ]
  118.     then
  119.         exit
  120.     fi
  121. }
  122.  
  123. trap "bye"
  124.  
  125. getinfokey() {
  126.     if [ ! -z "$package_info" ]
  127.     then
  128.         grep -i "^[     ]*$1[     ]*" $package_info | sed -n "s/^[     ]*[^     ]*[     ]*\\(.*\\)/\\1/p"
  129.     fi
  130. }
  131.  
  132. while [ $# -ne 0 ]
  133. do
  134.     case "$1" in
  135.     --help)
  136.         usage 0
  137.         ;;
  138.     --version)
  139.         echo "$me version $version, by Yves Arrouye"
  140.         exit 0
  141.         ;;
  142.     --limitations)
  143.         limitations
  144.         exit 0
  145.         ;;
  146.     --interactive)
  147.         interactive=yes
  148.         ;;
  149.     --automatic)
  150.         interactive=
  151.         ;;
  152.     --verbose)
  153.         verbose=1
  154.         ;;
  155.     --install)
  156.         action=install
  157.         ;;
  158.     --dwiw)
  159.         dwiw=1
  160.         ;;
  161.     --noreceipt)
  162.         receipt=0
  163.         ;;
  164.     --delete)
  165.         action=delete
  166.         ;;
  167.     --info)
  168.         action=info
  169.         ;;
  170.     --list)
  171.         action=list
  172.         ;;
  173.     -*)
  174.         echo "Unknown option: $1"
  175.         usage;;
  176.     *)
  177.         if [ -z "$package" ]
  178.         then
  179.         package="$1"
  180.         else
  181.         if [ -z "$installdir" ]
  182.         then
  183.             installdir="$1"
  184.         else
  185.             usage
  186.         fi
  187.         fi
  188.         ;;
  189.     esac
  190.     shift
  191. done
  192.  
  193. if [ -z "$package" ]
  194. then
  195.     usage
  196. fi
  197.  
  198. if [ $action = "delete" -a ! -z "$installdir" ]
  199. then
  200.     usage
  201. fi
  202.  
  203. if [ ! -d /Library/Receipts ]
  204. then
  205.     2>/dev/null mkdir -p /Library/Receipts
  206. fi
  207. if [ ! -w /Library/Receipts ]
  208. then
  209.     receiptsdir=$HOME/Library/Receipts
  210. else
  211.     receiptsdir=/Library/Receipts
  212. fi
  213.  
  214. languages=`2>/dev/null defaults read NSGlobalDomain NSLanguages`
  215. : ${languages="(English, French, German, Spanish, Italian, Swedish)"}
  216. languages=`echo "$languages" | sed 's/[^a-zA-Z]/ /g'`
  217.  
  218. findfile() {
  219.     for language in $languages
  220.     do
  221.     if [ -r "$2"/$language.lproj/"$3"."$1" ]
  222.     then
  223.         echo "$2"/$language.lproj/"$3"."$1"
  224.         return
  225.     fi
  226.     done
  227.  
  228.     if [ -r "$2"/"$3"."$1" ]
  229.     then
  230.     echo "$2"/"$3"."$1"
  231.     else
  232.     for lproj in `find "$2" -name \*.lproj -print`
  233.     do
  234.         if [ -r "$lproj"/"$3"."$1" ]
  235.         then
  236.         echo "$lproj"/"$3"."$1"
  237.         return
  238.         fi
  239.     done
  240.     fi
  241. }
  242.  
  243. findinfo() {
  244.     findfile info "$1" "$2"
  245. }
  246.  
  247. findarchive() {
  248.     for _compressExt in .gz .Z
  249.     do
  250.     for _archExt in .pax .tar
  251.     do
  252.             if [ -f "$1"/"$2"${_archExt}${_compressExt} ]
  253.             then
  254.                 echo "$1"/"$2"${_archExt}${_compressExt}
  255.             fi
  256.     done
  257.     done
  258. }
  259.  
  260. check_uid() {
  261.     if [ "`whoami`" = root ]
  262.     then
  263.     UID=0
  264.     else
  265.     if [ "$UID" = "" ]
  266.     then
  267.         UID=-1
  268.     fi
  269.     fi
  270.  
  271.     export UID
  272. }
  273.  
  274. check_auth() {
  275.     needsauth=`getinfokey NeedsAuthorization | tr '[a-z]' '[A-Z]'`
  276.  
  277.     if [ "$needsauth" = YES ]
  278.     then
  279.      : ${action:="mess with"}
  280.  
  281.         check_uid
  282.  
  283.     if [ $UID != 0 ]
  284.     then
  285.         >&2 echo $me: you must be root to $action this package
  286.         exit 7
  287.     fi
  288.     fi
  289. }
  290.  
  291. run_script() {
  292.     _scriptoutput=${TMPDIR}/.`basename "$1"`.$$
  293.     _scriptexit=${TMPDIR}/.`basename "$1"`.exit.$$
  294.     (sh -c "$1 $2 $3" 2>&1; echo $? >$_scriptexit) | tee $_scriptoutput
  295.     if [ ! -f $_scriptexit ]
  296.     then
  297.     _exitcode=1
  298.     else
  299.         _exitcode=`cat $_scriptexit`
  300.     fi
  301.     if [ -z "`2>/dev/null cat $_scriptoutput`" ]
  302.     then
  303.     if [ $_exitcode -eq 0 ]
  304.     then
  305.         echo "OK."
  306.     else
  307.         echo "ERR (script exited with code $_exitcode)."
  308.     fi
  309.     fi
  310.     /bin/rm -f $_scriptoutput $_scriptexit
  311.     if [ $_exitcode -ne 0 ]
  312.     then
  313.     exit $_exitcode
  314.     fi
  315. }
  316.  
  317. install_package() {
  318.     if [ ! -d $package ]
  319.     then
  320.     if [ ! -f $package ]
  321.     then
  322.         >&2 echo $me: $package does not exist
  323.     else
  324.             >&2 echo $me: $package does not look like an Installer package
  325.     fi
  326.         exit 2
  327.     else
  328.         case "$package" in
  329.         /*)
  330.             ;;
  331.         *)
  332.             package=`/bin/pwd`/$package
  333.             ;;
  334.         esac
  335.         package_name=`basename $package .pkg`
  336.  
  337.     # check for "new" format package
  338.         if [ -d $package/Contents ]
  339.         then
  340.                 package=$package/Contents/Resources
  341.                 >&2 echo $me: new format detected for $package
  342.         fi
  343.  
  344.         package_info=`findinfo $package $package_name`
  345.     echo "Package Info File: $package_info"
  346.  
  347.     src_location=`getinfokey SourceLocation`
  348.     if [ -z "$src_location" ]
  349.     then
  350.             package_archive=`findarchive $package $package_name`
  351.             if [ ! -f "$package_info" -o ! -f "$package_archive" ]
  352.             then
  353.         >&2 echo $me: $package lacks components required for its installation: $package_info or $package_archive
  354.         exit 2
  355.             fi
  356.     else
  357.             case "$src_location" in
  358.         /*)
  359.                 ;;
  360.         *)
  361.             # .. is needed because a relative src_location is relative
  362.             # to package loc, not package contents
  363.                 src_location=$package/../$src_location
  364.             ;;
  365.             esac
  366.             if [ ! -f "$package_info" -o ! -d "$src_location" ]
  367.             then
  368.         >&2 echo $me: $package lacks components required for its installation: $package_info or $src_location
  369.         exit 2
  370.             fi
  371.     fi
  372.  
  373.         package_sizes=$package/$package_name.sizes
  374.         package_bom=$package/$package_name.bom
  375.  
  376.         if [ ! -f $package_sizes -o ! -f $package_bom ]
  377.         then
  378.             >&2 echo $me: $package lacks components required by Installer.app
  379.         >&2 echo $me: $package will be installed anyway, but should be corrected
  380.         fi
  381.     fi
  382.  
  383.     relocatable=`getinfokey Relocatable | tr '[a-z]' '[A-Z]'`
  384.  
  385.     if [ "$relocatable" != YES -a ! -z "$installdir" ]
  386.     then
  387.     if [ $dwiw -eq 0 ]
  388.     then
  389.             >&2 echo $me: package $package is not relocatable
  390.             exit 3
  391.     fi
  392.     fi
  393.  
  394.     default_location=`getinfokey DefaultLocation`
  395.     : ${installdir:=$default_location}
  396.  
  397.     if [ -z "$src_location" ]
  398.     then
  399.         # this used to look for installer.app to use it's tar versions
  400.         uses_gnutar=1
  401.  
  402.         if [ $uses_gnutar -ne 1 ]
  403.         then
  404.             long_file_names=`getinfokey LongFileNames | tr '[a-z]' '[A-Z]'`
  405.             case "$long_file_names" in
  406.                 YES*)
  407.                     installer_tar=$installer_bin/installer_bigtar
  408.                     ;;
  409.                 *)
  410.                     installer_tar=$installer_bin/installer_tar
  411.                     ;;
  412.             esac
  413.         else
  414.             gnutar=`which gnutar`
  415.             : ${gnutar:=/usr/bin/gnutar}
  416.             if [ ! -x "$gnutar" ]
  417.             then
  418.                 >&2 echo $me: "couldn't execute \`$gnutar' for gnutar"
  419.                 #exit 7
  420.  
  421.                 # if theres no gnutar, use normal tar
  422.  
  423.                 reg_tar=`which tar`
  424.                 : ${reg_tar:=/usr/bin/tar}
  425.  
  426.                 if [ ! -x $reg_tar ]; then
  427.                     >&2 echo $me: "couldn't execute \`$reg_tar' for tar"
  428.                     exit 7
  429.                 else
  430.                     installer_tar=$reg_tar
  431.                     installer_tar_opts=p
  432.                 fi                   
  433.  
  434.             else
  435.                 installer_tar=$gnutar
  436.                 installer_tar_opts=p
  437.             fi
  438.         fi
  439.  
  440.         case "$installer_tar" in
  441.             gnutar|*/gnutar)
  442.                 uses_gnutar=1
  443.                 ;;
  444.             *)
  445.                 uses_gnutar=0
  446.                 ;;
  447.         esac
  448.  
  449.         uses_pax=0
  450.         case "$package_archive" in
  451.             *.Z)
  452.                 package_tar=$TMPDIR/`basename $package_archive .Z`
  453.                 ;;
  454.             *.pax.gz)
  455.         uses_pax=1
  456.         ;;
  457.             *.gz)
  458.                 package_tar=$TMPDIR/`basename $package_archive .gz`
  459.                 ;;
  460.         esac
  461.     fi
  462.  
  463.     needs_copy=0
  464.  
  465.     pre_install=`findfile pre_install $package $package_name`
  466.     post_install=`findfile post_install $package $package_name`
  467.  
  468.     if [ -f "$pre_install" -o -f "$post_install" ]
  469.     then
  470.         if [ ! -z "$interactive" ]
  471.         then
  472.         echo "This package contains script that will be run during the installation..."
  473.         echo -n "Do you want to run the scripts? [y] "
  474.         read ans
  475.  
  476.         case "$ans" in
  477.             y*|Y*|"")
  478.             ;;
  479.             *)
  480.             if [ $dwiw -ne 0 ]
  481.             then
  482.             echo "Scripts not run. Installation may be incomplete."
  483.             scripts=no
  484.             else
  485.                 echo "Aborting installation."
  486.                 exit 0
  487.             fi
  488.             ;;
  489.         esac
  490.         fi
  491.     fi
  492.  
  493.     check_auth
  494.  
  495.     echo Installing package $package
  496.     if [ ! -d $installdir ]
  497.     then
  498.     if [ -x /bin/mkdirs ]
  499.     then
  500.         /bin/mkdirs $installdir
  501.     else
  502.         /bin/mkdir -p $installdir
  503.     fi
  504.     code=$?
  505.     if [ $code -ne 0 ]
  506.     then
  507.         exit $code
  508.     fi
  509.     fi
  510.  
  511.     if cd $installdir
  512.     then
  513.     :
  514.     else
  515.     >&2 echo $me: cannot cd to $installdir
  516.     exit 4
  517.     fi
  518.  
  519.     if [ -f "$pre_install" ]
  520.     then
  521.     if [ "$scripts" != no ]
  522.     then
  523.             echo -n Running pre_install script...\ 
  524.         run_script $pre_install $package $installdir
  525.     else
  526.         echo Not running pre_install script.
  527.     fi
  528.     fi
  529.  
  530.     if [ "$UID" = 0 ]
  531.     then
  532.     # we'll use ditto
  533.     fi
  534.  
  535.     if [ -z "$src_location" ]
  536.     then
  537.     echo -n Extracting package contents...\ 
  538.     tarerrs=${TMPDIR}/.$me.tar.$$
  539.     trap "/bin/rm -f $tarerrs; bye" 1 2 3 15
  540.     if [ "$uses_pax" = 1 ]
  541.     then
  542.             /bin/pax -z -r -pe -f $package_archive 2>$tarerrs
  543.         code=$?
  544.     else
  545.             if [ $needs_copy -eq 1 ]
  546.             then
  547.                 echo -n Copying... \
  548.                 cp $package_archive $TMPDIR
  549.                 /bin/rm -f $package_tar
  550.                 gzip -d $package_archive
  551.                 $installer_tar x${installer_tar_opts}f $package_tar 2>$tarerrs
  552.             else
  553.                 if [ $uses_gnutar -eq 1 ]
  554.                 then
  555.                     $installer_tar xz${installer_tar_opts}f $package_archive 2>$tarerrs
  556.                 else
  557.                     gzip -dc $package_archive | $installer_tar x${installer_tar_opts}f - 2>$tarerrs
  558.                 fi
  559.             fi
  560.             code=$?
  561.  
  562.             /bin/rm -f $package_tar
  563.         fi
  564.  
  565.         if [ $code -ne 0 ]
  566.         then
  567.             echo 1>&2 "ERR (extract failed)."
  568.             if [ $verbose -ne 0 ]
  569.             then
  570.                 echo Log follows:
  571.                 cat $tarerrs
  572.             fi
  573.             /bin/rm -f $tarerrs
  574.             exit $code
  575.         else
  576.             echo OK.
  577.         fi
  578.     /bin/rm -f $tarerrs
  579.     else    # we have a src path, so use ditto
  580.     echo -n Extracting package contents...\ 
  581.     dittoerrs=${TMPDIR}/.$me.ditto.$$
  582.     trap "/bin/rm -f $dittoerrs; bye" 1 2 3 15
  583.     if [ $verbose -ne 0 ]
  584.     then
  585.         dittoFlags=-V
  586.         echo
  587.         echo Source:      $src_location
  588.         echo Destination: `pwd`
  589.     fi
  590.     if [ -r $package_bom ]
  591.     then
  592.         dittoBOMFlags="-bom $package_bom"
  593.     fi
  594.     ditto $dittoFlags $dittoBOMFlags $src_location . 2>$dittoerrs
  595.  
  596.     code=$?
  597.     if [ $code -ne 0 ]
  598.     then
  599.         echo "ERR (ditto failed)."
  600.         if [ $verbose -ne 0 ]
  601.         then
  602.         echo Ditto log follows:
  603.         cat $dittoerrs
  604.         fi
  605.         /bin/rm -f $dittoerrs
  606.         exit $code
  607.     else
  608.         echo OK.
  609.     fi
  610.     fi
  611.  
  612.     if [ -f "$post_install" ]
  613.     then
  614.     if [ "$scripts" != no ]
  615.     then
  616.             echo -n Running post_install script...\ 
  617.             run_script $post_install $package $installdir
  618.     else
  619.         echo Not running post_install script.
  620.     fi
  621.     fi
  622.    
  623.     if [ $receipt -ne 0 ]
  624.     then
  625.     if [ ! -d $receiptsdir ]
  626.     then
  627.         2>/dev/null mkdir -p $receiptsdir
  628.     fi
  629.         if [ ! -w $receiptsdir ]
  630.         then
  631.             receipt_failed=yes
  632.         else
  633.             if [ -d $receiptsdir/$package_name.pkg ]
  634.             then
  635.             if [ ! -w $receiptsdir/$package_name.pkg ]
  636.             then
  637.             receipt_failed=yes
  638.             fi
  639.             fi
  640.     fi
  641.  
  642.     if [ -z "$receipt_failed" ]
  643.     then
  644.             echo -n "Creating receipt for package in $receiptsdir... "
  645.             /bin/rm -rf $receiptsdir/$package_name.pkg
  646.             if [ -x /bin/mkdirs ]
  647.             then
  648.                 /bin/mkdirs $receiptsdir/$package_name.pkg
  649.             else
  650.                 /bin/mkdir -p $receiptsdir/$package_name.pkg
  651.             fi
  652.             for f in info bom sizes tiff
  653.             do
  654.                 if [ -f $package/$package_name.$f ]
  655.                 then
  656.                     cp $package/$package_name.$f $receiptsdir/$package_name.pkg
  657.                 fi
  658.             done
  659.         for f in software_version
  660.         do
  661.                 if [ -f $package/$f ]
  662.         then
  663.                     cp $package/$f $receiptsdir/$package_name.pkg
  664.         else
  665.             if [ -f /System/Library/CoreServices/$f ]
  666.             then
  667.             cp /System/Library/CoreServices/$f $receiptsdir/$package_name.pkg
  668.             fi
  669.         fi
  670.         done
  671.         if false
  672.         then
  673.             if [ ! -f $receiptsdir/$package_name.pkg/$package_name.info ]
  674.             then
  675.             firstinfo=`find $package -name \*.info -print | head -1`
  676.             if [ ! -z "$firstinfo" ]
  677.             then
  678.                 cp $firstinfo $receiptsdir/$package_name.pkg
  679.             fi
  680.             fi
  681.         else
  682.             for d in `find $package -name \*.lproj -print`
  683.             do
  684.                 (cd $package; gnutar cf - `basename $d`) | (cd $receiptsdir/$package_name.pkg; gnutar xpf -)
  685.             done
  686.         fi
  687.         fi
  688.  
  689.         for f in pre_install post_install pre_delete post_delete
  690.         do
  691.             if [ -f $package/$package_name.$f ]
  692.             then
  693.             cp $package/$package_name.$f $receiptsdir/$package_name.pkg
  694.             fi
  695.         done
  696.         echo "$installdir" >$receiptsdir/$package_name.pkg/$package_name.location
  697.         echo "installed" >$receiptsdir/$package_name.pkg/$package_name.status
  698.  
  699.     echo OK.
  700.     else
  701.     >&2 echo $me: cannot write a receipt for the package in $receiptsdir
  702.     exit 4
  703.     fi
  704. }
  705.  
  706. delete_package() {
  707.     echo "Deleting: $package"
  708.     if [ ! -d $package ]
  709.     then
  710.     if [ ! -f $package ]
  711.     then
  712.         >&2 echo $me: $package does not exist
  713.     else
  714.             >&2 echo $me: $package does not look like an Installer package
  715.     fi
  716.         exit 2
  717.     else
  718.         case "$package" in
  719.         /*)
  720.             ;;
  721.         *)
  722.             package=`/bin/pwd`/$package
  723.             ;;
  724.         esac
  725.  
  726.     package_base_dir=$package
  727.         package_name=`basename $package .pkg`
  728.  
  729.     # check for "new" format package
  730.     if [ -d $package/Contents ]
  731.     then
  732.         package=$package/Contents/Resources
  733.         >&2 echo $me: new format detected for $package
  734.     fi
  735.  
  736.         package_info=`findinfo $package $package_name`
  737.     echo "Package Info File: $package_info"
  738.  
  739.         package_location=$package/$package_name.location
  740.     if [ ! -r $package_location ]
  741.     then
  742.         package_location=$package/$package_name.loc
  743.         if [ ! -r $package_location ]
  744.         then
  745.             # almost last try: /Library/Receipts/$package/$package_name.location 
  746.             package=/Library/Receipts/$package_name.pkg
  747.             package_receipts_dir=$package
  748.             package_location=$package/$package_name.location
  749.             if [ ! -r $package_location ]
  750.             then
  751.                 # last try: /Library/Receipts/$package/Contents/Resources/$package_name.log
  752.                 package=/Library/Receipts/$package_name.pkg/Contents/Resources
  753.                 package_location=$package/$package_name.loc
  754.                 if [ ! -r $package_location ]
  755.                 then
  756.                     >&2 echo $me: $package does not look like an installed package
  757.                     exit 3
  758.                 fi
  759.             fi
  760.         fi
  761.     fi
  762.  
  763.     installdir="`cat $package_location`"
  764.  
  765.     echo "Installed in Directory: $installdir"
  766.  
  767.     if [ -z "$installdir" -o ! -d "$installdir" ]
  768.     then
  769.         if [ $dwiw -ne 1 ]
  770.         then
  771.             >&2 echo $me: $package has an incorrect installation directory
  772.             exit 3
  773.         else
  774.         installdir=`getinfokey Location`
  775.         if [ -z "$installdir" -o ! -d "$installdir" ]
  776.         then
  777.             >&2 echo $me: cannot figure out the installation directory for $package
  778.         else
  779.             >&2 echo $me: warning: assuming package $package was installed in its $installdir default location
  780.         fi
  781.         fi
  782.     fi
  783.  
  784.     package_bom=$package/$package_name.bom
  785.  
  786.     if [ ! -r $package_bom ]
  787.     then
  788.         >&2 echo $me: $package lacks a bill of materials and cannot be delete
  789.         exit 3
  790.     fi
  791.  
  792.     check_auth
  793.  
  794.     installonly=`getinfokey InstallOnly | tr '[a-z]' '[A-Z]'`
  795.     if [ "$installonly" = YES ]
  796.     then
  797.         if [ $dwiw -ne 1 ]
  798.         then
  799.             >&2 echo $me: $package can only be installed, not deleted
  800.             exit 4
  801.         fi
  802.     fi
  803.  
  804.     deletewarning=`getinfokey DeleteWarning`
  805.     if [ ! -z "$deletewarning" ]
  806.     then
  807.         if [ ! -z "$interactive" ]
  808.         then
  809.         echo $deletewarning
  810.         echo -n "Do you really want to delete this package? [y] "
  811.         read ans
  812.  
  813.             case "$ans" in
  814.                 y*|Y*|"")
  815.                 ;;
  816.                 *)
  817.                 if [ $dwiw -ne 0 ]
  818.                 then
  819.                 echo "Scripts not run. Deletion may be incomplete."
  820.                 scripts=no
  821.                 else
  822.                     echo "Aborting deletion."
  823.                     exit 0
  824.             fi
  825.                 ;;
  826.             esac
  827.         fi
  828.     fi
  829.  
  830.         pre_delete=`findfile pre_delete $package $package_name`
  831.         post_delete=`findfile post_delete $package $package_name`
  832.  
  833.         if [ -f "$pre_delete" -o -f "$post_delete" ]
  834.         then
  835.             if [ ! -z "$interactive" ]
  836.             then
  837.             echo "This package contains script that will be run during the deletion..."
  838.             echo -n "Do you want to run the scripts? [y] "
  839.             read ans
  840.     
  841.             case "$ans" in
  842.                 y*|Y*|"")
  843.                 ;;
  844.                 *)
  845.                 echo "Aborting deletion"
  846.                 exit 0
  847.                 ;;
  848.             esac
  849.             fi
  850.         fi
  851.  
  852.     echo Deleting $package
  853.  
  854.         if [ -f "$pre_delete" ]
  855.         then
  856.         if [ "$scripts" != no ]
  857.         then
  858.                 echo -n Running pre_delete script...\ 
  859.             run_script $pre_delete $package $installdir
  860.         else
  861.             echo Not running pre_delete script.
  862.         fi
  863.         fi
  864.  
  865.     dirsfile=${TMPDIR}/.$me.$$
  866.  
  867.     /bin/rm -f $dirsfile
  868.     trap "/bin/rm -f $dirsfile; bye" 1 2 3 15
  869.  
  870.     echo -n Deleting package contents...\ 
  871.  
  872.     for i in `$lsbom $package_bom | sed 's/[     ][     ]*[0-9].*//'`
  873.     do
  874.         what="$installdir"/"$i"
  875.  
  876.         if [ -d "$what" ]
  877.         then
  878.         if [ ! -z $verbose ] 
  879.         then
  880.             echo "Dir: $what"
  881.         fi
  882.         (cd "$what"; /bin/pwd) >>$dirsfile
  883.         else
  884.         if [ ! -z $verbose ] 
  885.         then
  886.             echo "File: $what"
  887.         fi
  888.         2>/dev/null /bin/rm -f "$what"
  889.         code=$?
  890.         if [ $code -ne 0 ]
  891.         then
  892.             echo "ERR (removing $what)"
  893.             exit $code
  894.         fi
  895.         fi
  896.     done
  897.  
  898.     for i in `sort -r $dirsfile`
  899.     do
  900.         if [ -d "$i" ]
  901.         then
  902.         if [ ! -z $verbose ]
  903.         then
  904.             echo "Remove Dir: $i"
  905.         fi
  906.         2>/dev/null /bin/rmdir "$i"
  907.         fi
  908.     done
  909.  
  910.         /bin/rm -f $dirsfile
  911.  
  912.         if [ -f "$post_delete" ]
  913.         then
  914.         if [ "$scripts" != no ]
  915.         then
  916.                 echo -n Running post_delete script...\ 
  917.             run_script $post_delete $package $installdir
  918.         else
  919.             echo Not running post_delete script.
  920.         fi
  921.         fi
  922.  
  923.     echo Deleting $package
  924.  
  925.     /bin/rm -rf $package
  926.     if [ -d $package_receipts_dir ] 
  927.     then
  928.         if [ ! -z $verbose ]
  929.         then
  930.             echo "rm receipts dir: $package_receipts_dir"
  931.             echo "Package receipts directory: $package_receipts_dir"
  932.         fi
  933.         #/bin/rm -rf $package_receipts_dir
  934.     fi
  935.     if [ -d $package_base_dir ]
  936.     then
  937.         if [ ! -z $verbose ]
  938.         then
  939.             echo "rm base_dir: $package_base_dir"
  940.         fi
  941.         /bin/rm -rf $package_base_dir
  942.     fi
  943.     fi
  944. }
  945.  
  946. info_package() {
  947.     if [ ! -d $package ]
  948.     then
  949.     if [ ! -f $package ]
  950.     then
  951.         >&2 echo $me: $package does not exist
  952.     else
  953.             >&2 echo $me: $package does not look like an Installer package
  954.     fi
  955.         exit 2
  956.     else
  957.         case "$package" in
  958.         /*)
  959.             ;;
  960.         *)
  961.             package=`/bin/pwd`/$package
  962.             ;;
  963.         esac
  964.         package_name=`basename $package .pkg`
  965.  
  966.         package_info=`findinfo $package $package_name`
  967.  
  968.         if [ ! -f "$package_info" ]
  969.     then
  970.         >&2 echo $me: $package does not have a readable info file
  971.         exit 3
  972.     fi
  973.  
  974.         title=`getinfokey Title`
  975.     if [ ! -z "$title" ]
  976.     then
  977.         echo $title
  978.     fi
  979.         version=`getinfokey Version`
  980.     if [ ! -z "$version" ]
  981.     then
  982.         echo Version: $version
  983.     fi
  984.         descr=`getinfokey Description`
  985.     if [ ! -z "$descr" ]
  986.     then
  987.         echo Description: $descr
  988.     fi
  989.     if [ -r $package/software_version ]
  990.     then
  991.         echo Software Version: `cat $package/software_version`
  992.     fi
  993.         package_archive=`findarchive $package $package_name`
  994.     case "$package_archive" in
  995.         *.Z)
  996.         if [ $verbose -ne 0 ]
  997.         then
  998.             echo Archive: Apparently made with BSD compress
  999.         fi
  1000.         ;;
  1001.         *.gz)
  1002.         if [ $verbose -ne 0 ]
  1003.         then
  1004.             echo "Archive: Apparently made with GNU gzip"
  1005.         fi
  1006.         ;;
  1007.     esac
  1008.  
  1009.     src_location =`getinfokey SourceLocation`
  1010.     if [ ! -z "$src_location" ]
  1011.     then
  1012.         if [ $verbose -ne 0 ]
  1013.         then
  1014.         echo Non-archive: source located at $src_location
  1015.         fi
  1016.     fi
  1017.  
  1018.     status="not installed"
  1019.     if [ -r $package/$package_name.status ]
  1020.     then
  1021.         status=`cat $package/$package_name.status`
  1022.         if [ -r $package/$package_name.location ]
  1023.         then
  1024.         status="$status (in `cat $package/$package_name.location`)"
  1025.         fi
  1026.     else
  1027.             default_location=`getinfokey DefaultLocation`
  1028.             relocatable=`getinfokey Relocatable`
  1029.         if [ ! -z default_location ]
  1030.         then
  1031.         status="$status (will install in $default_location"
  1032.         if [ "$relocatable" = YES ]
  1033.         then
  1034.             status="$status by default"
  1035.         fi
  1036.         status="$status)"
  1037.         fi
  1038.     fi
  1039.     echo Status: `echo $status | sed 's/^\(.\).*/\1/' | tr '[a-z]' '[A-Z]'``echo $status | sed 's/.\(.*\)$/\1/'`
  1040.     fi
  1041. }
  1042.  
  1043. list_package() {
  1044.     if [ ! -d $package ]
  1045.     then
  1046.     if [ ! -f $package ]
  1047.     then
  1048.         >&2 echo $me: $package does not exist
  1049.     else
  1050.             >&2 echo $me: $package does not look like an Installer package
  1051.     fi
  1052.         exit 2
  1053.     else
  1054.         case "$package" in
  1055.         /*)
  1056.             ;;
  1057.         *)
  1058.             package=`/bin/pwd`/$package
  1059.             ;;
  1060.         esac
  1061.         package_name=`basename $package .pkg`
  1062.  
  1063.         package_bom=$package/$package_name.bom
  1064.  
  1065.         if [ ! -r $package_bom ]
  1066.     then
  1067.         >&2 echo $me: $package does not have a bill of materials
  1068.         exit 3
  1069.     fi
  1070.  
  1071.         package_location=$package/$package_name.location
  1072.  
  1073.     if [ -r "$package_location" ]
  1074.     then
  1075.         installdir="`cat $package_location`"
  1076.     fi
  1077.  
  1078.     $lsbom $package_bom | \
  1079.         sed -e 's/[     ][     ]*[0-9].*//' \
  1080.         -e 's,^\./,,' -e "s,^,$installdir,"
  1081.     fi
  1082. }
  1083.  
  1084. ${action}_package
  1085.  
  1086.